package com.example.shop.search

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.widget.SearchView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.base.entity.GoodsEntity
import com.example.base.entity.HistroyEntity
import com.example.base.mvvm.BaseActivity
import com.example.shop.R
import com.example.shop.adapter.GoodsAdapter
import com.example.shop.databinding.ActivitySearchBinding
import com.example.shop.db.AppDbUtils

@Route(path = "/app/SearchActivity")
class SearchActivity : BaseActivity<ActivitySearchBinding,SearchViewModel>() {
    val list = mutableListOf<GoodsEntity>()
    lateinit var goodsAdapter: GoodsAdapter
    @SuppressLint("MissingInflatedId")
    override fun initView() {
        goodsAdapter = GoodsAdapter(list)
        binding.rvSearch.adapter = goodsAdapter
        binding.rvSearch.layoutManager = LinearLayoutManager(this)

        val his = AppDbUtils.getAppDb().getHistoryDao().queryHis()
        if(his.isNotEmpty()){
            binding.cgHistory.removeAllViews()
            his.forEach {
                val key = it.keywords
                val chip = layoutInflater.inflate(R.layout.item_history,null);
                chip.findViewById<TextView>(R.id.key).text = it.keywords
                binding.cgHistory.addView(chip)
                //点击chip,实现搜索功能
                chip.setOnClickListener {
                    binding.searview.setQuery(key,true)
                }
            }
        }


        binding.searview.setOnQueryTextListener(object: SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                if(query!=null){
                    AppDbUtils.getAppDb().getHistoryDao().insertHis(HistroyEntity(query))
                    viewModel.search(query)
                }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                return true
            }
        })
        binding.ivBack.setOnClickListener {
            finish()
        }

    }

    override fun initData() {
        viewModel.searchSuccess.observe(this){
            //历史搜索隐藏
            binding.cgHistory.isVisible = false
            list.clear()
            if(it.isNotEmpty()){
                list.addAll(it)
                goodsAdapter.notifyDataSetChanged()
            }else{
                ToastUtils.showLong("未查询到符合条件的数据")
            }
        }
        viewModel.searchFail.observe(this){
            ToastUtils.showLong(it)
        }

    }

    override fun getViewModel(): Class<SearchViewModel> = SearchViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_search

}